<?php
namespace Home\Controller;
use Think\Controller;

class AutoplayController extends CommonController {


    function curl_post($url, $data, $header, $post = 1)
    {
        //初始化curl
        $ch = curl_init();
        //参数设置
        $res = curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, $post);
        if ($post)
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $result = curl_exec($ch);
        //连接失败
        if ($result == FALSE) {

            $result = "{\"statusCode\":\"172001\",\"statusMsg\":\"网络错误\"}";

        }
    }

    function smsnl($mobile, $content)
    {
        date_default_timezone_set('PRC');//设置时区
        $url = 'http://www.api.zthysms.com/sendSms.do';
        $data['username'] = 'jiangyuyunhy';
        $data['tkey'] = date('YmdHis');
        $data['password'] = md5(md5('eMV5qU') . $data['tkey']);
        $data['mobile'] = $mobile;
        $data['content'] = $content;

        $res = $this->curl_post($url, http_build_query($data));
    }

    function mypush($content, $vid = '', $title = '系统信息')
    {

        $client = new \Home\Service\JPush\Client(C('PUSH_KEY'), C('PUSH_STR'));//安卓
        $client_ios = new \Home\Service\JPush\Client(C('PUSH_KEY'), C('PUSH_STR'));//ios
        $result = $client
            ->push()
            ->setPlatform(array('android', 'ios'))
            ->options(['apns_production' => true]);
        $result_ios = $client_ios
            ->push()
            ->setPlatform(array('android', 'ios'))
            ->options(['apns_production' => true]);
        //options参数，true表示生产环境，文档里写的默认是生产环境是有错误的，必须手动设置
        //vid为空的话表示群发
        if (empty($vid)) {
//            $result->addAllAudience();
        } else {
            $result->addAlias(trim($vid));
        }
        if (empty($vid)) {
            $result_ios->addAllAudience();
        } else {
            $result_ios->addAlias(trim($vid));
        }
        if (empty($vid)) {
            $type = '2';
        } else {
            $type = '1';
        }
        $adddata = array(
            'title' => $title,
            'content' => $content,
            'ctime' => time(),
            'status' => '1',
            'vid' => $vid,
            'type' => $type,
        );
        M('user_news', '', C('DB'))->add($adddata);
        //ios端是没有标题的，只有安卓端才有标题
        $res = $result
            ->iosNotification($content, [
                'sound' => 'sound',
                'badge' => '+1',
                'extras' => [
                    'key' => 'value'
                ]
            ])
            ->androidNotification($content, [
                'title' => $title,
                'extras' => [
                    'key' => 'value'
                ]
            ])
            ->send();
        $ress = $result_ios
            ->iosNotification($content, [
                'sound' => 'sound',
                'badge' => '+1',
                'extras' => [
                    'key' => 'value'
                ]
            ])
            ->androidNotification($content, [
                'title' => $title,
                'extras' => [
                    'key' => 'value'
                ]
            ])
            ->send();
    }

    function anrongborrow($id_card, $truename, $order, $type, $money)
    {//成功借款
        $applyCreditCity = substr($id_card, 0, 6);
        $url = 'https://www.allwincredit.cn/mspRemote.shtml?member=1935&sign=uzYIxoaFyUueo&customerName=' . $truename . '&paperNumber=' . $id_card . '&loanId=' . $order . '&loanTypeDesc=jiekuan&applyDate=' . date('Y-m-d') . '&applyCreditCity=' . $applyCreditCity . '&applyLoanMoney=' . $money . '&applyLoanTimeLimit=1&applyAssureType=D&checkResultTime=' . date('Y-m-d') . '&checkResult=01&loanMoney=' . $money . '&loanStartDate=' . date('Y-m-d', time()) . '&loanEndDate=' . date('Y-m-d', time() + $type * 24 * 60 * 60) . '&loanCreditCity=' . $applyCreditCity . '&loanPeriods=1&loanAssureType=D';
        return $url;
    }
    function getcurl($url = '')
    {

        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        return $data;
    }
    public function test(){
        $redis = new \Redis();
        $redis->pconnect(C('REDIS_POST'), 6379);
        $redis->auth(C('REDIS_PWD'));

    }
    public function edit_limit(){
        $redis = new \Redis();
        $redis->pconnect(C('REDIS_POST'), 6379);
        $redis->auth(C('REDIS_PWD'));
        $now_money_eight = M('user_borrow_check','',C('ZHENGXIN'))->where(array('status'=>'1','is_bespeak'=>'1','type'=>'1'))->sum('money');
        $now_money_eight = empty($now_money_eight)?0:$now_money_eight;
        $now_money_fifteen=M('user_borrow_check','',C('ZHENGXIN'))->where(array('status'=>'1','is_bespeak'=>'1','type'=>'2'))->sum('money');
        $now_money_fifteen = empty($now_money_fifteen)?0:$now_money_fifteen;
        $ago_money = M('limit_quota','',C('CAIWU'))->where(array('dtime'=>date('Y-m-d',time())))->field('borrow_money_eight,borrow_money_fifteen')->find();
        $limit = M('limit_quota','',C('CAIWU'))->where(array('dtime'=>date('Y-m-d',time())))->field('money_eight,money_fifteen')->find();
        $eight_res = $limit['money_eight']-$now_money_eight-$ago_money['borrow_money_eight'];
        $fifteen_res = $limit['money_fifteen']-$now_money_fifteen-$ago_money['borrow_money_fifteen'];
        $eight_res = $eight_res<=200?'a':$eight_res;
        $fifteen_res = $fifteen_res<=200?'b':$fifteen_res;
        $redis->set('eight_quota_limit',$eight_res);
        $redis->set('fifteen_quota_limit',$fifteen_res);
    }
    public function add_borrow_check(){
        $redis=new \Redis();
        $redis->pconnect(C('REDIS_POST'), 6379);
        $redis->auth(C('REDIS_PWD'));
        $res=$redis->lRange('user_borrow_check',0,-1);
        $count=count($res);
        if(empty($res)){
            exit();
        }
        $redis->lTrim('user_borrow_check',$count,-1);
        $tly=$redis->lRange('user_borrow_tly',0,-1);
        foreach ($tly as $k=>$v){
            $add1 = json_decode($v,true);
            M('user_gyro','',C('DB'))->add($add1);
            $redis->rPop('user_borrow_tly');
        }
        foreach ($res as $k=>$v){
            $a = json_decode($v,true);
            $add[$k] = $a;
            $appl = $redis->get('appl'.$add[$k]['vid']);
            $app = explode(',',$appl);
            $apps = M('user_applys','',C('DB'))->where(array('vid'=>$add[$k]['vid']))->getField('name',true);
            foreach ($app as $a=>$b){
                if(!in_array($app[$a],$apps)){
                    $addapp = array(
                        'vid'=>$add[$k]['vid'],
                        'name'=>$b,
                        'ctime'=>time()
                    );
                    $addappn[$a]=$addapp;
                }
            }
            M('user_applys','',C('DB'))->addALL($addappn);
            $redis->delete('appl'.$add[$k]['vid']);
            $userdata = M('user','',C('DB_ZD'))->where(Array('vid'=>$add[$k]['vid']))->find();//            print_r($userdata);die;
            unset($addnum);
            $special_user  = M('special_user ','',C('DB'))->where(array('mobile'=>$userdata['mobile'],'status'=>'1'))->getField('id');
            if(empty($special_user)){
                $addcheck = array('H0001','H0002','H0004','H0005','M0028','M0029','F0033','H0036','H0040','H0041','H0042','Z0001','Z0002','Z0003','Z0004','Z0005','Z0006','Z0007','Z0008','Z0011','Z0010','Z0012','Z0013','Z0014','Z0015');
                foreach ($addcheck as $key => $value) {
                    $addchecklist = array(
                        'vid'=>$add[$k]['vid'],
                        'number'=>$addcheck[$key],
                        'status'=>'0',
                    );
                    $addnum[$key] = $addchecklist;
                }
                M('user_examine_repay','',C('ZHENGXIN2'))->addALL($addnum);
            }else{
                $addchecklist = array(
                    'vid'=>$add[$k]['vid'],
                    'number'=>'Z0009',
                    'status'=>'0',
                );
                M('user_examine_repay','',C('ZHENGXIN2'))->add($addchecklist);
            }

            unset($addnum);
            unset($ret);
            unset($ret1);
//            $redis->lPop('user_borrow_check');
        }
//        print_r($add);die;
        M('user_borrow_check','',C('ZHENGXIN'))->addALL($add);

    }
}